#include "link_list.h"
#include "fmt_prt.h"

int32_t list_push(struct linked_list* list, struct linked_node* node)
{
    if(!node) {
        //
        fmt_printf("%s failed, node == NULL\n", __func__);
        return -1;
    }
    if(!list) {
        //
        fmt_printf("%s failed, list == NULL\n", __func__);
        return -1;
    }

    if(list->count == 0) {
        list->head = node;
    } else {
        node->next = list->head;
        list->head = node;
    }
    list->count++;
    return 0;
}

int32_t list_pop(struct linked_list* list, struct linked_node* node)
{
    if(!node) {
        //
        fmt_printf("%s failed, node == NULL\n", __func__);
        goto failed;
    }
    if(!list) {
        //
        fmt_printf("%s failed, list == NULL\n", __func__);
        goto failed;
    }
    if(list->count == 0) {
        //
        fmt_printf("%s failed, list count == 0\n", __func__);
        goto failed;
    }

    struct linked_node* pre,* cur;
    uint32_t count;

    count = list->count;
    pre = NULL;
    cur = list->head;
    if(cur == node) goto find_it;

    count--;
    while (count--)
    {
        pre = cur;
        cur = cur->next;
        if(cur == node) goto find_it;
    }

failed:
    // not found

    return -1;

find_it:
    if(!pre) {
        list->head = node->next;
    }else {
        pre->next = node->next;
    }
    return 0;
}